<?php
namespace app\admin\controller;

use app\admin\model\Arctype as ArctypeModel;
use app\admin\model\Article as ArticleModel;
use my\Tree;
use Request;

/**
 * 用户分信息
 */
class Article extends Base
{
    /*
    显示分类信息列表
     */
    public $data;
    public function index($act = "")
    {
        $where = [];
        if (Request::has('search')) {
            $where[] = ['title', 'like', '%' . Request::param('search/s', '', 'strip_tags') . '%'];
        }
        //$list = ArticleModel::alias('a')->field('a.*,b.title as typename')->join('arctype b', 'b.id = a.typeid', 'left')->order('id desc')->where($where)->paginate(20);
        $list   = ArticleModel::order('id desc')->where($where)->paginate(config('webconfig.page'), false, ['query' => Request::param()]);
        $search = Request::param('search/s', '', 'strip_tags') ? Request::param('search/s', '', 'strip_tags') : "";
        $this->assign([
            'list'   => $list,
            'page'   => $list->render(),
            'search' => $search,
        ]);
        return $this->fetch();
    }
    /*
    信息添加
     */
    public function add()
    {
        if ($this->request->isPost()) {

            $post = $this->request->post();

            $result = $this->validate($post, 'app\admin\validate\Article');
            if (true !== $result) {
                return $this->error($result);
            }

            $data = ArticleModel::create($post);
            if (!empty($data)) {
                return $this->success('添加成功', 'admin/Article/index');
            } else {
                return $this->error('1添加失败');
            }
        }
        $data = ArctypeModel::select();
        $tree = new Tree();
        $data = $tree->tree($data, 'title');
        $this->assign('typename', $data);
        return $this->fetch();
    }

    /*
    信息编辑
     */
    public function edit()
    {
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if ($this->request->isPost()) {

            $post = $this->request->post();

            $result = $this->validate($post, 'app\admin\validate\Article.edit');
            if (true !== $result) {
                return $this->error($result);
            }

            $model = ArticleModel::update($post);
            if ($model) {
                return $this->success('修改成功', 'admin/Article/index');
            } else {
                return $this->error('1修改失败');
            }
        }

        $info = ArticleModel::get($id);
        if (empty($info)) {return $this->error('id参数错误');}

        $data = ArctypeModel::select();
        $tree = new Tree();
        $data = $tree->tree($data, 'title');
        //去掉子栏目（防止，选择子分类）
        //print_r($data);
        $this->assign([
            'typename' => $data,
            'info'     => $info,
        ]);
        return $this->fetch();
    }

    /*
    信息删去
     */
    public function delall($ids = '')
    {
        $ids   = !empty($ids) ? $ids : '';
        $data   = ArticleModel::destroy($ids);
        if (!empty($data)) {
            return $this->success('删去成功', 'admin/Article/index');
        }
        return $this->error('删去失败');
    }

    /*
    信息删去
     */
    public function delete($value = '')
    {
        $id     = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        $data   = ArticleModel::get($id); // 获取要删除的对象  不存在时返回 null    $user::destroy($id) //批量删时尽量用，返回记录条数，没有删除返回0
        if (!empty($data)) {
            if ($data->delete()) {
                return $this->success('删去成功', 'admin/Article/index');
            }
        }
        return $this->error('删去失败');
    }

}
